namespace DCHealthCheck.DomainModel {

    #region Usings
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Diagnostics;
    using System.Text;
    
    #endregion

    [Table("PendingReplication")]
    public class PendingReplication : IPendingReplication, IObjectWithState, IValidatableObject {

        #region Members
        #region Max length fields
        public static readonly int OperationTypeMaxLength = 1024;
        public static readonly int PartitionNameMaxLength = 1024;
        public static readonly int SourceServerMaxLength = 255;
        #endregion

        public virtual long Id { get; set; }
        public virtual int OperationNumber { get; set; }
        public virtual string OperationType { get; set; }
        public virtual string PartitionName { get; set; }
        public virtual int Priority { get; set; }
        public virtual string SourceServer { get; set; }
        public virtual DateTime TimeEnqueued { get; set; }

        #region Related Entities
        [ForeignKey("SystemInfo")]
        public virtual Guid SystemInfoId {
            get {
                if (this.SystemInfo != null) return this.SystemInfo.Id;
                return systemInfoId;
            }
            set { systemInfoId = value; }
        }
        private Guid systemInfoId;

        public virtual SystemInfo SystemInfo {
            get { return systemInfo; }
            set {
                systemInfo = value;
                if (systemInfo != null) {
                    if (this.SystemInfoId != systemInfo.Id) {
                        this.SystemInfoId = systemInfo.Id;
                    }
                }
            }
        }
        private SystemInfo systemInfo;
        #endregion

        #region NotMapped properties
        [NotMapped]
        public DetachedState DetachedState { get; set; }

        [NotMapped]
        public Object EntityKey { get { return this.Id; } }

        [NotMapped]
        public Dictionary<string, object> StartingOriginalValues { get; set; }
        #endregion 
        #endregion

        #region Constructor
        public PendingReplication() {
            this.OperationNumber = -1;
            this.OperationType = string.Empty;
            this.PartitionName = string.Empty;
            this.Priority = -1;
            this.SourceServer = string.Empty;
        } 
        #endregion

        #region Methods
        [DebuggerStepThroughAttribute]
        public override string ToString() {
            var info = new StringBuilder();

            info.Append("PendingReplication: ");

            if ((this.SystemInfo != null) && (this.SystemInfo.DomainController != null) && (this.SystemInfo.DomainController.DNSName != null)) {
                info.AppendFormat("DC: {0}; ", this.SystemInfo.DomainController.DNSName);
            }
            if (this.SystemInfo != null) {
                info.AppendFormat("HealthCheck Date: {0}; ", this.SystemInfo.CheckDateTimeUTC.ToString("yyyy-MMM-dd"));
            }
            info.AppendFormat("Source Server: {0}; ", this.SourceServer != null ? this.SourceServer : "NULL");
            info.AppendFormat("Partition Name: {0}; ", this.PartitionName != null ? this.PartitionName : "NULL");
            info.AppendFormat("Time Enqueued: {0}", this.TimeEnqueued.ToString("yyyy-MMM-dd HH:mm"));

            return info.ToString();
        }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
            if ((this.DetachedState != DetachedState.Added) && (this.Id < 1)) {
                yield return new ValidationResult(
                    "Id is required.", new[] { "Id" });
            }

            if (this.DetachedState != DetachedState.Deleted) {
                // add entity to SystemInfo, let DetectChanges perform relationship fixup
                if (this.SystemInfoId == Guid.Empty) {
                    yield return new ValidationResult(
                        "SystemInfoId is required.", new[] { "SystemInfoId" });
                }

                if ((this.SystemInfo != null) && (this.SystemInfo.Id != this.SystemInfoId)) {
                    yield return new ValidationResult(
                        "SystemInfo.Id does not match SystemInfoId.", new[] { "SystemInfo, SystemInfoId" });
                }

                if (this.OperationNumber < 0) {
                    yield return new ValidationResult(
                        "OperationNumber is required.", new[] { "OperationNumber" });
                }

                if (string.IsNullOrWhiteSpace(this.OperationType)) {
                    yield return new ValidationResult(
                        "OperationType is required.", new[] { "OperationType" });
                }
                else if (this.OperationType.Length > OperationTypeMaxLength) {
                    yield return new ValidationResult(string.Format("OperationType length: {0} exceeds max length: {1}",
                        this.OperationType.Length, OperationTypeMaxLength), new[] { "OperationType" });
                }

                if (string.IsNullOrWhiteSpace(this.PartitionName)) {
                    yield return new ValidationResult(
                        "PartitionName is required.", new[] { "PartitionName" });
                }
                else if (this.PartitionName.Length > PartitionNameMaxLength) {
                    yield return new ValidationResult(string.Format("PartitionName length: {0} exceeds max length: {1}",
                        this.PartitionName.Length, PartitionNameMaxLength), new[] { "PartitionName" });
                }

                if (this.Priority < 0) {
                    yield return new ValidationResult(
                        "Priority is required.", new[] { "Priority" });
                }

                if (this.SourceServer == null) {
                    yield return new ValidationResult(
                        "SourceServer is required.", new[] { "SourceServer" });
                }
                else if (this.SourceServer.Length > SourceServerMaxLength) {
                    yield return new ValidationResult(string.Format("SourceServer length: {0} exceeds max length: {1}",
                        this.SourceServer.Length, SourceServerMaxLength), new[] { "SourceServer" });
                }
            } // if (this.DetachedState != State.Deleted)
        }  
        #endregion
        
    }
}
